//
//  ArrayExtensions.swift
//  FoundationCustom
//
//  Created by ZhouJiatao on 2018/12/10.
//  Copyright © 2018 Unknow. All rights reserved.
//

import Foundation

public extension Array {
    /// 通过索引获取元素。如果索引越界，则返回nil
    func element(at anyIndex: Int) -> Element? {
        if anyIndex >= self.count || anyIndex < 0 {
            return nil
        }
        
        return self[anyIndex]
    }
    
    /// 交集
    func intersect(with other: [Element], where predicate: ((_ e1:Element, _ e2: Element) -> Bool)) -> [Element] {
        var result: [Element] = []
        for myItem in self {
            for otherItem in other {
                if(predicate(myItem, otherItem)) {
                    result.append(myItem)
                }
            }
        }
        return result
    }
    
}
